package com.hcqc.demo.server;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcqc.demo.mapper.Form1Mapper;
import com.hcqc.demo.pojo.Form1;
import com.hcqc.demo.vo.DateUtil;
import com.hcqc.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class Form1ServerImpl implements Form1Server {
    @Autowired
    Form1Mapper form1Mapper;

    @Override
    public List<Form1> SelectALL(String user_name, Form1 search) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("DRR", user_name);
        if(search!=null){
            //查询条件
            if (search.getNAME()!=null&&search.getNAME()!="")
                queryWrapper.eq("NAME", search.getNAME());
            if (search.getSNAME()!=null&&search.getSNAME()!="")
                queryWrapper.eq("SNAME", search.getSNAME());
            if (search.getTNAME()!=null&&search.getTNAME()!="")
                queryWrapper.eq("TNAME", search.getTNAME());
        }
        List<Form1> list = form1Mapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public void addFrom1Info(List<Form1> form1List) {
        for (Form1 i:form1List)
            form1Mapper.insert(i);
    }

    @Override
    public void addFrom1Info(List<Form1> form1List, String user_name) {
        //添加导入人和导入时间
        for (Form1 form:form1List){
            form.setDRR(user_name).setDRSJ(DateUtil.GetNow());
        }
        for (Form1 i:form1List)
            form1Mapper.insert(i);
    }

    @Override
    public void delete(String IDs) {
        String[] split = IDs.split(",");
        List<Integer> ids = new ArrayList<>();
        for(String s:split){
            ids.add(Integer.valueOf(s));
        }
        form1Mapper.deleteBatchIds(ids);
    }

    @Override
    public void addOne(Form1 form1) {
        form1Mapper.insert(form1);
    }

    @Override
    public List<Form1> SelectIDs(String user_name, String iDs) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("DRR", user_name);
        queryWrapper.inSql("ID", iDs);
        List<Form1> list = form1Mapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public Result selectAllAndPage(String user_name, Form1 search, Integer pageIndex, Integer pageSize) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("DRR", user_name);
        if(search!=null){
            //查询条件
            if (search.getNAME()!=null&&search.getNAME()!="")
                queryWrapper.eq("NAME", search.getNAME());
            if (search.getSNAME()!=null&&search.getSNAME()!="")
                queryWrapper.eq("SNAME", search.getSNAME());
            if (search.getTNAME()!=null&&search.getTNAME()!="")
                queryWrapper.eq("TNAME", search.getTNAME());
        }
        //分页条件
        Page page=new Page(pageIndex, pageSize);
        IPage iPage=form1Mapper.selectPage(page, queryWrapper);
        long total = iPage.getTotal();
        List list = iPage.getRecords();
        return Result.success("",list, (int) total);
    }
}
